﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace QLNHK.BLL
{
    public partial class NhanKhau
    {
        private static string GetMaxMaNhanKhau()
        {
            const string spName = "SELECT TOP 1 MaNhanKhau FROM t_NhanKhau ORDER BY MaNhanKhau DESC";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetSqlStringCommand(spName);

            object val = db.ExecuteScalar(dbCommand);
            return val != null ? val.ToString() : string.Empty;
        }
        public static string GetNewMaNhanKhau()
        {
            string maNhanKhau_MoiNhat = NhanKhau.GetMaxMaNhanKhau();

            return string.Format("NK{0}", ((string.IsNullOrEmpty(maNhanKhau_MoiNhat) ? 0 : System.Convert.ToInt32(maNhanKhau_MoiNhat.Replace("NK", ""))) + 1).ToString("00000000"));
        }

        public static DataSet SearchBy(string whereCondition, string orderByExpression)
        {
            const string spName = "p_NhanKhau_SearchBy";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);

            return db.ExecuteDataSet(dbCommand);
        }
    }
}